/*
 * Copyright (c) 2012 Johann Hanne
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef NO_HEAP

#include <stdlib.h>

#include "FreeRTOS.h"
#include "task.h"

#include "bget.h"

/*#define DEBUGF(fmt, ...) printf(fmt, __VA_ARGS__)*/
#define DEBUGF(fmt, ...)

void __heapinit(void) {
    bpool((void *)0x2000:>0x0000, 0x10000l);
    bpool((void *)0x2100:>0x0000, 0x10000l);
    bpool((void *)0x2200:>0x0000, 0x10000l);
    bpool((void *)0x2300:>0x0000, 0x10000l);
    bpool((void *)0x2400:>0x0000, 0x10000l);
    bpool((void *)0x2500:>0x0000, 0x10000l);
    bpool((void *)0x2600:>0x0000, 0x10000l);
    bpool((void *)0x2700:>0x0000, 0x10000l);
    bpool((void *)0x2800:>0x0000, 0x10000l);
    bpool((void *)0x2900:>0x0000, 0x10000l);
    bpool((void *)0x2a00:>0x0000, 0x10000l);
    bpool((void *)0x2b00:>0x0000, 0x10000l);
    bpool((void *)0x2c00:>0x0000, 0x10000l);
    bpool((void *)0x2d00:>0x0000, 0x10000l);
    bpool((void *)0x2e00:>0x0000, 0x10000l);
    bpool((void *)0x2f00:>0x0000, 0x10000l);
}

void *pvPortMalloc(size_t xWantedSize) {
    void *ret;

    vTaskSuspendAll();
    ret = bget((bufsize)xWantedSize);
    xTaskResumeAll();

    DEBUGF("pvPortMalloc(%zu) = %p\n", xWantedSize, ret);

    return ret;
}

void vPortFree(void *pv) {
    DEBUGF("vPortFree(%p)\n", pv);

    if (pv != NULL) {
        vTaskSuspendAll();
        brel(pv);
        xTaskResumeAll();
    }
}

void *calloc(size_t nmemb, size_t size) {
    void *ret;

    vTaskSuspendAll();
    ret = bgetz((bufsize)((bufsize)nmemb * (bufsize)size));
    xTaskResumeAll();

    DEBUGF("calloc(%zu, %zu) = %p\n", nmemb, size, ret);

    return ret;
}

void *malloc(size_t size) {
    void *ret;

    vTaskSuspendAll();
    ret = bget((bufsize)size);
    xTaskResumeAll();

    DEBUGF("malloc(%zu) = %p\n", size, ret);

    return ret;
}

void free(void *ptr) {
    DEBUGF("free(%p)\n", ptr);

    if (ptr != NULL) {
        vTaskSuspendAll();
        brel(ptr);
        xTaskResumeAll();
    }
}

void *realloc(void *ptr, size_t size) {
    void *ret;

    vTaskSuspendAll();
    if (ptr == NULL) {
        ret = bget((bufsize)size);
    } else {
        if (size == 0) {
            brel(ptr);
            ret = NULL;
        } else {
            ret = bgetr(ptr, (bufsize)size);
        }
    }
    xTaskResumeAll();

    DEBUGF("realloc(%p, %zu) = %p\n", ptr, size, ret);

    return ret;
}

#endif /* NO_HEAP */
